package com.team.sns.web.inteceptor;

import java.lang.reflect.Method;
import java.util.List;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.team.sns.annotations.Permission;
import com.team.sns.entities.User;
import com.team.sns.services.impl.UserServiceBean;
public class PermissionInterceptor implements Interceptor {

	private static final long serialVersionUID = -4807942909183701853L;
	

	public void destroy() {

	}

	public void init() {

	}

	@SuppressWarnings("unchecked")
	public String intercept(ActionInvocation invocation) throws Exception {
		String invokeMethodName = invocation.getProxy().getMethod();
		Method method = invocation.getAction().getClass().getDeclaredMethod(invokeMethodName, void.class);
		Permission permission = method.getAnnotation(Permission.class);
		String action = permission.action();

		User user = (User)invocation.getInvocationContext().getSession().get("user");
		List<String> permissions = UserServiceBean.PERMISSIONMAP.get(user.getId());
		
		if (permissions.contains(action)) {
			invocation.invoke();
		} else {
			System.out.println("-----------权限不够");
		}
		return null;
	}
}
